package com.example.shippxy.thread;

import com.alibaba.fastjson.JSONObject;
import com.example.shippxy.constant.Constant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(1)
public class SysInitThread implements CommandLineRunner {
    @Value("${ships}")
    private String ships;

    @Value("${port}")
    private String port;
    @Override
    public void run(String... args) throws Exception {
        //启动查询线程
        System.out.println("初始化数据");
        initData();
        System.out.println("启动系统线程");
        startThread();
    }

    /**
     * 初始化数据
     */
    private void initData(){
        // String ships = "{\"413523180\":\"五指山\",\"413523230\":\"尖峰岭\",\"413523240\":\"鹦哥岭\",\"413523220\":\"铜鼓岭\",\"413523210\":\"白石岭\",\"413523190\":\"黎母岭\",\"413522050\":\"海峡一号\",\"413521120\":\"宝岛12号\",\"413522220\":\"宝岛16号\",\"412522670\":\"信海12号\",\"413520260\":\"信海16号\",\"413522110\":\"信海19号\",\"413523990\":\"凤凰岭\",\"413524010\":\"六连岭\",\"413523980\":\"海棠湾\",\"413522480\":\"海口六号\",\"413523430\":\"海口九号\",\"413525630\":\"海口16号\",\"413234410\":\"海装8号\",\"413231340\":\"紫荆二十三\",\"413232860\":\"海峡二号\",\"413232890\":\"金紫荆\",\"413233380\":\"银紫荆\",\"413232470\":\"紫荆二十二\",\"413234440\":\"紫荆九号\",\"413232490\":\"紫荆十二\",\"413233350\":\"紫荆十六\",\"413232480\":\"紫荆十五\",\"413233370\":\"紫荆十一\",\"413233360\":\"双泰11\",\"413233560\":\"双泰12\",\"413231030\":\"双泰16\",\"413233570\":\"双泰18\",\"413233580\":\"双泰19\",\"413233590\":\"双泰26\",\"413233620\":\"双泰27\",\"413233240\":\"双泰28\",\"413233650\":\"双泰29\",\"413233630\":\"双泰36\",\"413233640\":\"双泰37\",\"413234380\":\"双泰宝昌\",\"413234430\":\"海装2号\",\"413234420\":\"海装6号\",\"413234390\":\"海装18号\",\"413234720\":\"南方6号\",\"413234660\":\"腾胜宝昌\",\"413234640\":\"扬帆海安\"}";
        JSONObject json = JSONObject.parseObject(ships);
        Constant.ships = json.getInnerMap();
        Constant.PORT = Integer.valueOf(port);
    }

    private void startThread(){
        ShipxyThread shipxyThread = new ShipxyThread();
        new Thread(shipxyThread).start();

        SocketThread socketThread = new SocketThread();
        new Thread(socketThread).start();
    }
}
